package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CustomerPersonAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CustomerPersonParams;
import com.isunland.managesystem.entity.CustomerRelationDepPerson;
import com.isunland.managesystem.entity.CustomerRelationDepPerson.DepartmentPerson;
import com.isunland.managesystem.entity.CustomerRelationKeyDepartment.DepartMentList;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 客户部门人员列表
 *
 * @author wu
 */
public class CustomerRelationStaffListFragment extends BaseListFragment {
  public static final String EXTRA_DEPARTMENT = "com.isunland.managesystem.ui.extra_department";
  public static final String NEW = "new";
  //public static final String NEW_BACK = "newback";
  //public static final String PUBLISH = "publish";
  public static final String SUBMIT = "submit";
  protected static final String TAG = CustomerRelationStaffListFragment.class.getSimpleName();
  private static final int REQUEST_ADD = 1;
  private static final int REQUEST_ALTER = 0;
  private static final int MENU_ITEM_ADD = 2;
  private CustomerPersonAdapter mAdapter;
  private CustomerPersonParams mParams;
  private ArrayList<DepartmentPerson> mList;
  private ArrayList<DepartmentPerson> mListOriginal;
  //private DepartMentList mDepartmentList;

  public static Fragment newInstance(DepartMentList department) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_DEPARTMENT, department);
    CustomerRelationStaffListFragment fragment = new CustomerRelationStaffListFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override public void initData() {
    super.initData();
    mParams = !(mBaseParams instanceof CustomerPersonParams) ? new CustomerPersonParams()
        : (CustomerPersonParams) mBaseParams;
    mList = new ArrayList<>();
    mListOriginal = new ArrayList<>();
    mAdapter = new CustomerPersonAdapter(mActivity, mList);
    mAdapter.setShowCustomerName(mParams.isShowCustomerName());
    mAdapter.setShowCallAndMessageMenu(mParams.isShowCallAndMessageMenu());
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setTitleCustom(R.string.customerContact1);
    setListAdapter(mAdapter);
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    //mDepartmentList = (DepartMentList) getArguments().get(EXTRA_DEPARTMENT);
    super.onCreate(savedInstanceState);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    if (mParams.isShowAddMenu()) {
      menu.add(Menu.NONE, MENU_ITEM_ADD, 1, R.string.add)
          .setIcon(R.drawable.add)
          .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    }
    if (mParams.isShowSearchMenu()) {
      inflater.inflate(R.menu.menu_search, menu);
      MenuItem menuItem = menu.findItem(R.id.menu_item_search);//在菜单中找到对应控件的item
      final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
      //android.support.v7.widget.SearchView.setOnQueryTextListener
      if (searchView == null) return;
      searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override public boolean onQueryTextSubmit(String query) {
          //ToastUtil.show("提交:"+query);
          return false;
        }

        @Override public boolean onQueryTextChange(String newText) {
          ArrayList<DepartmentPerson> result = search(newText);
          mList.clear();
          mList.addAll(result);
          mAdapter.notifyDataSetChanged();
          return false;
        }
      });
    }

    super.onCreateOptionsMenu(menu, inflater);
  }

  private ArrayList<DepartmentPerson> search(String newText) {
    ArrayList<DepartmentPerson> result = new ArrayList<>();
    if (MyStringUtil.isEmpty(newText)) {
      result = mListOriginal;
    } else {
      for (DepartmentPerson item : mListOriginal) {
        if (MyStringUtil.contains(item.getBlongCustomerName(), newText) || MyStringUtil.contains(
            item.getStaffName(), newText)) {
          result.add(item);
        }
      }
    }
    return result;
  }

  @Override public String getUrl() {

    return ApiConst.URL_STAFF;
  }

  @Override public HashMap<String, String> getparameters() {

    ParamsNotEmpty params = new ParamsNotEmpty();
    //params.put("blongOrgCode", blongOrgCode);

    //查询所有人
    params.put("getAllLinkMan", String.valueOf(mParams.isAllLinkMan()));
    //只查客户
    params.put("blongCustomerCode", mParams.getCustomerCode());
    params.put("curPage", "1");
    params.put("pageSize", "1000");
    LogUtil.i(TAG, "mBaseParams=" + params);
    LogUtil.i(TAG, "mUrl=" + ApiConst.URL_STAFF);
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    CustomerRelationDepPerson staff = new Gson().fromJson(json, CustomerRelationDepPerson.class);
    mListOriginal.addAll(staff.getDataList());
    mList.clear();
    mList.addAll(staff.getDataList());
    mAdapter.notifyDataSetChanged();
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    //String status = mDepartmentList.getDataStatus();
    switch (item.getItemId()) {
      case MENU_ITEM_ADD:
        CustomerPersonParams params = new CustomerPersonParams();
        params.setCustomerName(mParams.getCustomerName());
        params.setCustomerCode(mParams.getCustomerCode());
        BaseVolleyActivity.newInstance(this, CustomerRelationAddPersonActivity.class, params,
            REQUEST_ADD);

        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    DepartmentPerson mDepartmentPerson = mAdapter.getItem(position - 1);
    //点击显示电话菜单,不进入详情.
    if (mParams.isShowCallAndMessageMenu()) {
      mAdapter.initPopMenu(v.findViewById(R.id.iv_more), mDepartmentPerson);
      return;
    }
    Intent intent = new Intent(getActivity(), CustomerRelationStaffDetailActivity.class);
    intent.putExtra(CustomerRelationStaffDetailFragment.EXTRA_VALUE, mDepartmentPerson);
    startActivityForResult(intent, REQUEST_ALTER);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    switch (requestCode) {
      case REQUEST_ADD:

        volleyPost();
        break;
      case REQUEST_ALTER:

        volleyPost();
        break;

      default:
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
}
